-- Tags: long, no-tsan, no-asan, no-ubsan, no-msan, no-debug
DROP TABLE IF EXISTS t1;
DROP TABLE IF EXISTS t2;

CREATE TABLE t1 (key UInt32, s String) ENGINE = MergeTree ORDER BY key SETTINGS index_granularity = 8192, index_granularity_bytes = '10Mi';
CREATE TABLE t2 (key UInt32, s String) ENGINE = MergeTree ORDER BY key SETTINGS index_granularity = 8192, index_granularity_bytes = '10Mi';

{% set ltable_size = 10000000 -%}
{% set rtable_size = 1000000 -%}

INSERT INTO t1
    SELECT
        sipHash64(number, 'x') % {{ ltable_size }} + 1 as key,
        'val' || toString(number) as s
    FROM numbers_mt({{ ltable_size }})
;

INSERT INTO t2
    SELECT
        sipHash64(number, 'y') % {{ rtable_size }} + 1 as key,
        'val' || toString(number) as s
    FROM numbers_mt({{ rtable_size }})
;


{% for join_algorithm in ['full_sorting_merge', 'grace_hash'] -%}

SET max_bytes_in_join = '{% if join_algorithm == 'grace_hash' %}16M{% else %}0{% endif %}';

SELECT '-- {{ join_algorithm }} --';
SET join_algorithm = '{{ join_algorithm }}';

{% for kind in ['ALL', 'ANY'] -%}
{% for block_size in [10240, 32001, 65536] %}

SET max_block_size = {{ block_size }};


SELECT '{{ kind }} INNER';
SELECT sum(t1.key), sum(t2.key), count(), countIf(t1.key != 0), countIf(t2.key != 0) FROM t1
{{ kind }} INNER JOIN t2
ON t1.key == t2.key
;

SELECT '{{ kind }} LEFT';
SELECT sum(t1.key), sum(t2.key), count(), countIf(t1.key != 0), countIf(t2.key != 0) FROM t1
{{ kind }} LEFT JOIN t2
ON t1.key == t2.key
;

SELECT '{{ kind }} RIGHT';
SELECT sum(t1.key), sum(t2.key), count(), countIf(t1.key != 0), countIf(t2.key != 0) FROM t1
{{ kind }} RIGHT JOIN t2
ON t1.key == t2.key
;


{% endfor -%}
{% endfor -%}

SET max_bytes_in_join = 0;

{% endfor -%}
